﻿#region << 文件说明 >>
/*----------------------------------------------------------------
 * 命名空间：test.ViewModel
 * 唯一标识：a90367fa-e0ec-4d4c-928a-e22ebdb67726
 * 文件名：MiniApiController
 * 
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2023/12/18 12:53:59
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 文件说明 >>

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vampirewal.Core.Interface;
using Vampirewal.Core.MiniApi;
using Vampirewal.Core.SimpleMVVM;
using Vampirewal.Core.WebApi;

namespace test.ViewModel;


/// <summary>
/// 
/// </summary>
[Vampirewal.Core.MiniApi.VampirewalController(Prefix = "/MiniApi100")]
public partial class MiniApiController
{
    /// <summary>   
    /// 构造函数
    /// </summary>
    public MiniApiController()
    {

    }


    /// <summary>
    /// 测试获取
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    [HttpRouter( Vampirewal.Core.MiniApi.HttpMethod.GET)]
    public async Task<string> GetName(string name)
    {
        return await Task.Run(() =>
        {
            return $"{name}:你好！";
        });
    }

    /// <summary>
    /// 测试获取
    /// </summary>
    /// <param name="age">年龄</param>
    /// <returns></returns>
    [HttpRouter(Vampirewal.Core.MiniApi.HttpMethod.POST)]
    [TestFilter]
    public async Task<testInput> GetAge(testInput age)
    {
        return await Task.Run(() =>
        {
            age.Age = 20;

            return age;

        });
    }

    [HttpRouter( Vampirewal.Core.MiniApi.HttpMethod.GET)]
    public string GetAAA(string a, string b)
    {
        return $"{a}-{b}";
    }

    [HttpRouter( Vampirewal.Core.MiniApi.HttpMethod.GET)]
    public string GetBBB()
    {
        return $"asd";
    }

    

    #region [     属性     ]

    #endregion

    #region [     公开方法     ]

    #endregion

    #region [     私有方法     ]

    #endregion

    #region [     Command命令     ]

    #endregion

}

[Vampirewal.Core.MiniApi.VampirewalController(Prefix = "/MiniApi100")]
public class MiniApi2 
{
    private IVampirewalCoreDialogMessage Dialog { get; set; }
    private IDispatcherService Ds { get; set; }

    public MiniApi2(IVampirewalCoreDialogMessage dialog, IDispatcherService ds)
    {
        Dialog=dialog;
        Ds=ds;
    }


    [HttpRouter( Vampirewal.Core.MiniApi.HttpMethod.GET)]
    public string GetCCC()
    {
        Ds.BeginInvoke(() =>
        {
            Dialog.ShowPopupWindow(new Vampirewal.Core.WindoSetting.PopupWindowSetting()
            {
                Message = "测试弹窗",
                OwnerWindow = WindowsManager.GetInstance().MainWindow,
                Type = Vampirewal.Core.WindoSetting.MessageType.Successful
            });
        });

        return $"asd";
    }

    /// <summary>
    /// 测试异常拦截
    /// </summary>
    /// <exception cref="InvalidOperationException"></exception>
    [HttpRouter(Vampirewal.Core.MiniApi.HttpMethod.GET)]
    public IResult GetDDD()
    {
        //return Results.BadRequest();
        throw new InvalidOperationException("ceshi");
    }
}

/// <summary>
/// ceshi
/// </summary>
public class testInput
{
    /// <summary>
    /// 年龄
    /// </summary>
    public int Age { get; set; }
}

public class TestFilterAttribute : VampirewalMiniApiEndpointFilterAttribute
{
    public override async ValueTask<object> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
    {
        var parm= context.GetArgument<testInput>(0);

        if (parm.Age>18)
        {
            var result = await next(context);

            return result;
        }
        else
        {
            return Results.Problem("年龄小于18岁", null, 400, "错误", null) ;
        }

        
    }
}

public class testProvider : IVampirewalMiniApiResultProvider
{
    public async Task<ApiResult> OnErrorResultProvider(string data, int statusCode)
    {
        ApiResult result = new ApiResult()
        {
            IsSuccess = false,
            Code = statusCode,
            Message = data,
            Time = DateTime.Now,
        };

        return await Task.FromResult(result);
    }

    public async Task<IResult> OnResponseResultProvider(object? data, int statusCode)
    {

        Vampirewal.Core.MiniApi.ApiResult<object> result = new Vampirewal.Core.MiniApi.ApiResult<object>()
        {
            Data = data,
            IsSuccess = true,
            Code = statusCode,
            Time = DateTime.Now,
        };

        return await Task.FromResult(Results.Json(result));

    }
}